package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.quartz.dto.JobDTO;
import com.bolt.admin.module.quartz.dto.converter.JobConverter;
import com.bolt.admin.module.quartz.entity.JobEntity;
import com.bolt.admin.module.quartz.service.JobService;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2020-09-29 14:30:14
 *         定时任务
 **/
@Controller
@RequestMapping("/admin/quartz/job")
public class JobController extends BaseController <JobEntity, Long> {

    @Autowired
    private JobService jobService;


    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/quartz/job/index", request, model.asMap());
    }

    /**
     * 定时任务列表分页
     *
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage list(Pageable pageable, JobDTO dto) {
        PageInfo <JobEntity> pageInfo = jobService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dto, criteriaBuilder));
        return Returns.success(pageInfo.map(K -> JobConverter.toDTO(K)));

    }

    /**
     * 定时任务新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/quartz/job/input-index";
    }

    /**
     * 定时任务编辑页
     *
     * @param id    菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        JobEntity jobEntity = jobService.findOne(id).get();
        model.addAttribute(Form.PAGECONTEXT_FORMVALUES, JobConverter.toDTO(jobEntity));
        return "/admin/quartz/job/input-index";
    }

    /**
     * 定时任务编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="定时任务编辑操作")
    public ResultMessage saveOrUpdate(JobDTO dao) {
        jobService.saveOrUpdate(dao);
        return Returns.success("保存成功");
    }

    /**
     * 定时任务删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    @LogAnnotation(module = "系统管理",description ="定时任务删除操作")
    public ResultMessage doDelete(@RequestParam("id") Long id) {
        jobService.deleteById(id);
        return Returns.success("删除成功");
    }


}